<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;

use think\Config;

class Cron extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        // parent::_initialize();
    }

    // 算力
    public function index()
    {
        set_time_limit(0);
        ignore_user_abort(true);

        // 返佣日期
        $today = date('Ymd');
        // 等级配置信息
        $group_list = db('UserGroup')->column('id, static_rate, dynamic_max_num');

        // 静态算力
        $map = [
            'static_rebate_date'=>['lt', $today],
            'score'=>['gt', 0],
        ];
        $list = db('User')->field('id, group_id, score, money')->where($map)->select();
        if ($list) {
            foreach ($list as $key => $value) {
                if (!isset($group_list[$value['group_id']])) {
                    continue;
                }
                // 算力
                $rate = $group_list[$value['group_id']]['static_rate'];
                $money = $value['score']*$rate;
                model('app\common\model\User')->staticRebate($value['id'], $money, $value['money'], $today, '静态算力');
            }
        }

        // 分享算力
        //财富宝资产+智能宝资产总量小于此设置时，动态算力自动停止。
        $user_min_total_bkc = (int)config::get('site.user_min_total_bkc');
        $map = [
            'u1.dynamic_rebate_date'=>['lt', $today],
            'u2.group_id'=>['egt', 3],
        ];
        // 达到分享算力条件的会员
        $list = db('User u1')->field('u1.id, u1.group_id, count(u2.id) as child_num, u1.money')->join('__USER__ u2', 'u1.id=u2.parent_id')->where($map)->where('u1.score+u1.money>='.$user_min_total_bkc)->group('u1.id')->having('child_num>0')->select();
        if ($list) {
            foreach ($list as $key => $value) {
                if (!isset($group_list[$value['group_id']])) {
                    continue;
                }
                // 动态日封顶算力
                $max_num = $group_list[$value['group_id']]['dynamic_max_num'];
                // 分享算力配置
                $dynamic_rebate = [
                    1=>0.2,
                    2=>0.07,
                    3=>0.03,
                ];
                // 下线代数
                $child_num = $value['child_num']>3 ? 3 : $value['child_num'];
                $child_num = 3;
                // 下线
                $children = model('user')->getChildren($value['id'], $child_num);
                // 算力
                $total_money = 0;
                for ($i=1; $i <= $child_num; $i++) {
                    if (!isset($children[$i])) {
                        break;
                    }
                    foreach ($children[$i] as $k => $v) {
                        if (!isset($group_list[$v['group_id']])) {
                            continue;
                        }
                        //              下线的智能宝 * 下线的静态算力比例                           * 当前用户的动态算力比例
                        $total_money += $v['score'] * $group_list[$v['group_id']]['static_rate'] * $dynamic_rebate[$i];
                    }

                    /*echo'<pre>';
                    print_r($total_money);
                    echo'</pre>';*/
                }

                /*echo'<pre>';
                print_r($children);
                print_r($total_money);
                echo'</pre>';
                die;*/

                $total_money = $total_money>$max_num ? $max_num : $total_money;
                model('app\common\model\User')->dynamicRebate($value['id'], $total_money, $value['money'], $today, '分享算力');
            }
        }

        // 共享算力
        $list = model('User')->getTeamAmount();
        echo'<pre>';
        print_r($list);
        echo'</pre>';
        die;


        return '执行成功!';
    }
}
